﻿using System;

namespace KMLHelper
{
    public struct KMLVector3
    {
        /// <summary>
        /// 经度（单位为度）
        /// </summary>
        public double Longitude;
        /// <summary>
        /// 纬度（单位为度）
        /// </summary>
        public double Latitude;
        /// <summary>
        /// 海拔（单位为米）
        /// </summary>
        public double Elevation;
        
        public KMLVector3(double lon, double lat, double ele)
        {
            this.Longitude = lon;
            this.Latitude = lat;
            this.Elevation = ele;
        }

        public KMLVector3(KMLVector3 v)
        {
            this.Longitude = v.Longitude;
            this.Latitude = v.Latitude;
            this.Elevation = v.Elevation;
        }               

        /// <summary>
        /// 判断两个坐标是否相等
        /// </summary>
        /// <param name="u">KMLVector3.</param>
        /// <param name="v">KMLVector3.</param>
        /// <returns>返回是否相等</returns>
        public static bool operator ==(KMLVector3 u, KMLVector3 v)
        {
            return Equals(u, v);
        }

        /// <summary>
        /// 判断两个坐标是否不相等
        /// </summary>
        /// <param name="u">KMLVector3.</param>
        /// <param name="v">KMLVector3.</param>
        /// <returns>返回是否不相等</returns>
        public static bool operator !=(KMLVector3 u, KMLVector3 v)
        {
            return !Equals(u, v);
        }

        public override bool Equals(object obj)
        {
            if (obj is KMLVector3)
                return this.Equals((KMLVector3)obj);
            return false;
        }

        public bool Equals(KMLVector3 v)
        {
            return (Math.Abs(this.Longitude - v.Longitude) <= KMLConstant.Epsilon &&
                    Math.Abs(this.Latitude - v.Latitude) <= KMLConstant.Epsilon &&
                    Math.Abs(this.Elevation - v.Elevation) <= KMLConstant.Epsilon);
        }

        public override int GetHashCode()
        {
            return unchecked(this.Longitude.GetHashCode() ^ this.Latitude.GetHashCode() ^ this.Elevation.GetHashCode());
        }
    }
}
